package com.ruoyi.sfproduct.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.materials.domain.LwMaterial;
import com.ruoyi.materials.mapper.LwMaterialMapper;
import com.ruoyi.sfproduct.domain.vo.MaterialVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sfproduct.mapper.LwSfproductMapper;
import com.ruoyi.sfproduct.domain.LwSfproduct;
import com.ruoyi.sfproduct.service.ILwSfproductService;

/**
 * 半成品Service业务层处理
 * 
 * @author sll
 * @date 2023-08-09
 */
@Slf4j
@Service
public class LwSfproductServiceImpl implements ILwSfproductService 
{
    @Autowired
    private LwSfproductMapper lwSfproductMapper;

    @Autowired
    private LwMaterialMapper lwMaterialMapper;

    private Long sfpID;

    /**
     * 查询半成品
     * 
     * @param sfproductId 半成品主键
     * @return 半成品
     */
    @Override
    public LwSfproduct selectLwSfproductBySfproductId(Long sfproductId)
    {
        return lwSfproductMapper.selectLwSfproductBySfproductId(sfproductId);
    }

    /**
     * 查询半成品列表
     * 
     * @param lwSfproduct 半成品
     * @return 半成品
     */
    @Override
    public List<LwSfproduct> selectLwSfproductList(LwSfproduct lwSfproduct)
    {
        return lwSfproductMapper.selectLwSfproductList(lwSfproduct);
    }

    /**
     * 新增半成品
     * 
     * @param lwSfproduct 半成品
     * @return 结果
     */
    @Override
    public int insertLwSfproduct(LwSfproduct lwSfproduct)
    {
//        LwSfproduct lwSfproduct1 = lwSfproductMapper.selectLwSfproductBySfproductId(lwSfproduct.getSfproductId());
//        lwSfproduct.setSfproductName(lwSfproduct1.getSfproductName());
        return lwSfproductMapper.insertLwSfproduct(lwSfproduct);
    }

    /**
     * 修改半成品
     * 
     * @param lwSfproduct 半成品
     * @return 结果
     */
    @Override
    public int updateLwSfproduct(LwSfproduct lwSfproduct)
    {
        return lwSfproductMapper.updateLwSfproduct(lwSfproduct);
    }

    /**
     * 批量删除半成品
     * 
     * @param sfproductIds 需要删除的半成品主键
     * @return 结果
     */
    @Override
    public int deleteLwSfproductBySfproductIds(Long[] sfproductIds)
    {
        return lwSfproductMapper.deleteLwSfproductBySfproductIds(sfproductIds);
    }

    /**
     * 删除半成品信息
     * 
     * @param sfproductId 半成品主键
     * @return 结果
     */
    @Override
    public int deleteLwSfproductBySfproductId(Long sfproductId)
    {
        return lwSfproductMapper.deleteLwSfproductBySfproductId(sfproductId);
    }

    @Override
    public List<MaterialVo> selectMaterialVoBySfproductId(Long sfproductId) {
        return lwSfproductMapper.selectMaterialVoBySfproductId(sfproductId);
    }

    @Override
    public MaterialVo selectMaterialAndSfproduct(Long sfproductId, Long materialId) {
        return lwSfproductMapper.selectMaterialAndSfproduct(sfproductId,materialId);
    }

    @Override
    public int addMaterials(MaterialVo materialVo) {
        LwMaterial lwMaterial = lwMaterialMapper.selectLwMaterialByMaterialId(materialVo.getMaterialId());
        materialVo.setMaterialNumber(lwMaterial.getMaterialNumber());
        return lwSfproductMapper.addMaterials(materialVo);
    }

    @Override
    public int updateMaterials(MaterialVo materialVo) {
        return lwSfproductMapper.updateMaterials(materialVo);
    }

    @Override
    public int delMaterial(Long sfproductId, Long materialId) {
        return lwSfproductMapper.delMaterial(sfproductId,materialId);
    }

    @Override
    public int overAgain(Long sfproductId) {
        return lwSfproductMapper.overAgain(sfproductId);
    }

    @Log(title = "excel导入", businessType = BusinessType.IMPORT)
    @Override
    public String importData(List<MaterialVo> materialVoList, Boolean isUpdateSupport, String operName) throws Exception {
        if (StringUtils.isNull(materialVoList) || materialVoList.isEmpty())
        {
            throw new Exception("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<MaterialVo> existList = selectDatamaterialVoList();
        for (MaterialVo importData : materialVoList)
        {
            try {
                // 验证是否存在
                boolean userFlag = false;
                for (MaterialVo entry : existList) {
                    if (entry.getMaterialId().equals(importData.getMaterialId()) && entry.getSfproductId().equals(importData.getSfproductId())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertDataMaterialVo(importData);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、数据 ").append(importData.getMaterialNumber()).append(" 导入成功");
//                } else if (isUpdateSupport) {
////                    updateDataMaterialVo(importData);
//                    successNum++;
//                    successMsg.append("<br/>").append(successNum).append("、数据 ").append(importData.getMaterialNumber()).append(" 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、数据 ").append(importData.getMaterialNumber()).append(" 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、物料 " + importData.getMaterialNumber() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);

            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new Exception(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public void transmissionSfpID(Long sfproductId) {
        sfpID = sfproductId;
    }

//    private void updateDataMaterialVo(MaterialVo importData) {
//        LwMaterial lwMaterial = lwMaterialMapper.selectLwMaterialByMaterialNumber(importData.getMaterialNumber());
//        importData.setMaterialId(lwMaterial.getMaterialId());
//        importData.setSfproductId(sfpID);
//        lwSfproductMapper.updateMaterials(importData);
//    }

    private void insertDataMaterialVo(MaterialVo importData) {
        LwMaterial lwMaterial = lwMaterialMapper.selectLwMaterialByMaterialNumber(importData.getMaterialNumber());
        importData.setMaterialId(lwMaterial.getMaterialId());
        importData.setSfproductId(sfpID);
        importData.setEncapsulation(lwMaterial.getEncapsulation());
        lwSfproductMapper.addMaterials(importData);
    }

    private List<MaterialVo> selectDatamaterialVoList() {
        return lwSfproductMapper.selectDatamaterialVoList();
    }


}
